package zunge.twitterdel.foursquare;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.ResultSet;

import zunge.tools.DbConn;

public class LocationStat {
	static final String host = "localhost";
	static final String db_name = "oneweek";
	static final String user_name = "root";
	static final String password = "taipei101";
	
	static final int FRAG_0 = 421205;
	
	static DbConn conn;
	static FSAPI api;
	
	public static void main(String args[]){
		try{
			conn = new DbConn(host,db_name,user_name,password);
			api = new FSAPI();
			
			String log_undt = "/Users/bliu1/Desktop/undt.txt";
			String log_dt = "/Users/bliu1/Desktop/dt.txt";
			
			stat("undttweetwithlocation", log_undt, FRAG_0);
			stat("dttweetwithlocation", log_dt, 0);
			
			
		}catch(Exception e){e.printStackTrace();}
	}
	
	static void stat(String table_name, String logFile, int startIndex) throws Exception{
		String query = "select lat,lon from "+table_name+" order by tweetID";
		
		int cur = -1;
		ResultSet rs = conn.executeQuery(query);
		BufferedWriter bw = new BufferedWriter(new FileWriter(logFile));
		while(rs.next()){
			cur++;
			if(cur < startIndex){continue;}
			System.out.println((cur));
			//System.out.println((cur++)+"\t"+rootCat);//+"\t"+cat+"\t"+lat+"\t"+lon);
			
			String lat = rs.getString("lat");
			String lon = rs.getString("lon");
			String ll = lat+","+lon;
			
			String cat = api.getLocationCategory(ll);
			String rootCat = api.getRootCategory(cat);
			
			
			if(cat==null){cat="null";}
			if(rootCat==null){rootCat="null";}
			bw.write(rootCat+"\t"+cat+"\n");
			//bw.flush();
		}
		bw.close();
		rs.close();
	}
}
